﻿using System;
using System.ComponentModel;
using System.IO.IsolatedStorage;
using System.Windows;
using Microsoft.Phone.Shell;

namespace Pomodoro
{
    public class ViewModel : INotifyPropertyChanged
    {
        //RunsUnderLock
        //    Mute
        //    Vibrate

        public ViewModel()
        {
            if (!_lockedScreenEnabled) PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled;
            //LockedScreenEnabled = ((PhoneApplicationService.Current.ApplicationIdleDetectionMode = _lockedScreenEnabled ? IdleDetectionMode.Enabled : IdleDetectionMode.Disabled) == IdleDetectionMode.Enabled);
        }

        private bool _lockedScreenEnabled;
        public bool LockedScreenEnabled
        {
            get
            {
                if (!IsolatedStorageSettings.ApplicationSettings.TryGetValue("LockedScreenEnabled", out _lockedScreenEnabled))
                {
                    LockedScreenEnabled = false;
                }
                return _lockedScreenEnabled;
            }
            set
            {
                if (value != _lockedScreenEnabled)
                {
                    if (value)
                    {
                        if (MessageBox.Show("Running under lock screen consumes battery as the timer keeps ticking when you turn off the screen.\n\nHowever, the application keeps running even when the timer is not used.", "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                        {
                            PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled;
                        }
                        else value = false;
                    }

                    Helpers.Settings.Save("LockedScreenEnabled", _lockedScreenEnabled = value);
                    NotifyPropertyChanged("LockedScreenEnabled");
                }
            }
        }

        private bool _mute;
        public bool Mute
        {
            get
            {
                if (!IsolatedStorageSettings.ApplicationSettings.TryGetValue("Mute", out _mute))
                {
                    Mute = false;
                }
                return _mute;
            }
            set
            {
                if (value != _mute)
                {
                    Helpers.Settings.Save("Mute", _mute = value);
                    NotifyPropertyChanged("Mute");
                }
            }
        }

        private bool _vibrate;
        public bool Vibrate
        {
            get
            {
                if (!IsolatedStorageSettings.ApplicationSettings.TryGetValue("Vibrate", out _vibrate))
                {
                    Vibrate = true;
                }
                return _vibrate;
            }
            set
            {
                if (value != _vibrate)
                {
                    Helpers.Settings.Save("Vibrate", _vibrate = value);
                    NotifyPropertyChanged("Vibrate");
                }
            }
        }

        private Visibility _selfTimerIconVisibility = Visibility.Visible;
        public Visibility SelfTimerIconVisibility
        {
            get
            {
                return _selfTimerIconVisibility;
            }
            set
            {
                if (value != _selfTimerIconVisibility)
                {
                    _selfTimerIconVisibility = value;
                    NotifyPropertyChanged("SelfTimerIconVisibility");
                }
            }
        }

        private Visibility _settingsVisibility = Visibility.Collapsed;
        public Visibility SettingsVisibility
        {
            get
            {
                return _settingsVisibility;
            }
            set
            {
                if (value != _settingsVisibility)
                {
                    _settingsVisibility = value;
                    NotifyPropertyChanged("SettingsVisibility");
                }
            }
        }

        public bool SettingsVisible
        {
            get
            {
                return _settingsVisibility == Visibility.Visible;
            }

            set
            {
                SettingsVisibility = value ? Visibility.Visible : Visibility.Collapsed;
                SelfTimerIconVisibility = value ? Visibility.Collapsed : Visibility.Visible;

            }
        }

        /// <summary>
        /// Event for notifying subscribed binding handlers (INotifyPropertyChanged).
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }
}
